// Leader/pkiCenter/pkiCenter.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: [],
        activeTab: 0,
        defineNum: 4,
        content: [],
        startDate: "",
        endDate: "",
        pickEndDate: "",
        showPicker: false,
        timeParams: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const titles = ['今天', '昨天', '近一月', '累计']
        const tabs = titles.map(item => ({title: item}))
        let content = titles.map(item => ({title: item}));
        content.push({
            title : "自定义"
        })
        this.setData({tabs, content})
        const now = new Date();
        const pickEndDate = util.getDay(0, "-");
        this.setData({
            pickEndDate
        })
        const timeParams = this.getParams();
        timeParams.forEach((data, index) => {
            this.getDataList(index, data);
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getParams(){
        const now = new Date(),
            timeParams = [],
            today = util.getDay(0, "-"),
            yesterday = util.getDay(-1, "-"),
            currentYear = now.getFullYear(),
            currentMonth = now.getMonth();
        const No1 = new Date(currentYear, currentMonth, 1);
        // 今天
        timeParams.push({
            startDate: today,
            endDate: today
        })
        // 昨天
        timeParams.push({
            startDate: yesterday,
            endDate: yesterday
        })
        // 近一个月
        timeParams.push({
            startDate: util.formatTime(Date.parse(No1)/1000, "Y-M-D"),
            endDate: today
        })
        // 累计
        timeParams.push({})
        this.setData({timeParams});
        return timeParams;
    },
    getDataList(index, data){
        util.request(api.leader.achievementUrl, data)
        .then(res=>{
            const str = "content[" + index + "]";
            this.setData({
                [str]: res.result,
                showPicker: false
            })
        }).catch(err=>{

        })
    },
    onChange(e) {
        const index = e.detail.index
        this.setData({
            activeTab: index
        })
    },
    handleClick(e) {
        wx.navigateTo({
            url: './webview',
        })
    },
    scrolltolower(e){
        console.log("上拉加载")
    },
    switchTab(e){
        const id = e.currentTarget.dataset.id;
        this.setData({
            activeTab: id
        })
        if(id == this.data.defineNum){
            this.setData({
                showPicker: !this.data.showPicker
            })
        }else{
            if(this.data.showPicker){
                this.setData({
                    showPicker: false
                })
            }
        }
    },
    startDateChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          startDate: e.detail.value
        })
    },
    endDateChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          endDate: e.detail.value
        })
    },
    // 自定义时间
    defineFun(){
        
        const { startDate, endDate } = this.data;
        if(!(startDate && endDate)){
            wx.showToast({
                title: "日期还没选好哦",
                icon: "none"
            })
            return ;
        }
        if((startDate === endDate)){
            wx.showToast({
                title: "开始日期和结束日期不能同一天",
                icon: "none"
            })
            return ;
        }
        const defineParam = {
            startDate,
            endDate
        }
        const str = "timeParams["+this.data.defineNum+"]";
        this.setData({
            [str]:defineParam
        })
        this.getDataList(this.data.defineNum, defineParam);
    }
})